//
//  HMPhotoSelectorCell.swift
//  照片选择器
//
//  Created by iOS on 16/10/6.
//  Copyright © 2016年 iOS. All rights reserved.
//

import UIKit

protocol HMPhotoSelectorCellDelegate: NSObjectProtocol {
    func photoSelectorCellDidClickAddButton()
    func photoSelectorCellDidClickDeleteButton(cell : HMPhotoSelectorCell)
}

class HMPhotoSelectorCell: UICollectionViewCell {
    
    weak var delegate: HMPhotoSelectorCellDelegate?
    
    var image: UIImage? {
        didSet {
            // 将图片显示在加号按钮上面
            addButton.setImage(image, for: .normal)
            // 显示删除按钮
            deleteButton.isHidden = false
        }
    }
    
    // 设置加号按钮
    func setupAddButton() -> Void {
        addButton.setImage(UIImage(named: "compose_pic_add"), for: .normal)
        // 隐藏删除按钮
        deleteButton.isHidden = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    private func setupUI() {
        contentView.addSubview(addButton)
        contentView.addSubview(deleteButton)
        
        // 设置约束
        addButton.translatesAutoresizingMaskIntoConstraints = false
        deleteButton.translatesAutoresizingMaskIntoConstraints = false
        
        // VFL: Autolayout的可视化格式语言
        /* 
         H:水平方向约束
            |父控件左边 |父控件右边
            [view]: [view的左边, ]view的右边
         V:垂直方向约束
            |父控件顶部 |父控件底部
        */
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[ab]-0-|", options: [], metrics: nil, views: ["ab" : addButton]))
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[ab]-0-|", options: [], metrics: nil, views: ["ab" : addButton]))
        
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[db]-0-|", options: [], metrics: nil, views: ["db" : deleteButton]))
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[db]", options: [], metrics: nil, views: ["db" : deleteButton]))
    }
    
    func addButtonDidClick() -> Void {
        delegate?.photoSelectorCellDidClickAddButton()
    }
    
    func deleteButtonDidClick() -> Void {
        delegate?.photoSelectorCellDidClickDeleteButton(cell: self)
    }
    
    // 加号按钮
    private lazy var addButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setImage(UIImage(named: "compose_pic_add"), for: .normal)
        button.addTarget(self, action: #selector(addButtonDidClick), for: .touchUpInside)
        button.imageView?.contentMode = .scaleAspectFill
        
        return button
    }()
    
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setImage(UIImage(named: "compose_photo_close"), for: .normal)
        button.addTarget(self, action: #selector(deleteButtonDidClick), for: .touchUpInside)
        
        return button
    }()
}
